<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    
    <el-form-item label="所属省、市、县" prop="province">
      <!-- <el-input v-model="dataForm.province" placeholder="所属省"></el-input> -->
      <el-cascader :options="cityData" v-model="dataForm.address1"></el-cascader>
      <!-- <el-select v-model="value" placeholder="所属省">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select> -->
    </el-form-item>
    <!-- <el-form-item label="所属市" prop="city">
      <el-input v-model="dataForm.city" placeholder="所属市"></el-input>
    </el-form-item>
    <el-form-item label="所属县" prop="county">
      <el-input v-model="dataForm.county" placeholder="所属县"></el-input>
    </el-form-item> -->
    <el-form-item label="所属街道" prop="address">
      <el-input v-model="dataForm.address" placeholder="所属街道"></el-input>
    </el-form-item>
    <el-form-item label="村/社区" prop="name">
      <el-input v-model="dataForm.name" placeholder="村/社区名称"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="lon">
      <el-input v-model="dataForm.lon" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="维度" prop="lat">
      <el-input v-model="dataForm.lat" placeholder="维度"></el-input>
    </el-form-item>
    <el-form-item label="联系人" prop="contract">
      <!-- <el-input v-model="dataForm.contract" placeholder="联系人"></el-input> -->
      <el-select
        v-model="dataForm.contract"
        filterable
        remote
        reserve-keyword
        placeholder="联系人手机号"
        :remote-method="remoteMethod"
        :loading="loading">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import cityData from '../../../../static/js/citydata.js'
  export default {
    data () {
      return {
        visible: false,
        cityData,
        dataForm: {
          id: '',
          province: '',
          city: '',
          county: '',
          name: '',
          address: '',
          address1: [],
          contract: '',
          lon: '',
          lat: ''
        },
        loading: false,
        sysUsers: [],
        options: [],
        dataRule: {
          name: [
            { required: true, message: '村庄名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '街道地址不能为空', trigger: 'blur' }
          ],
          contract: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/country/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.dataForm.id = data.country.id
                this.dataForm.province = data.country.province
                this.dataForm.city = data.country.city
                this.dataForm.county = data.country.county
                this.dataForm.name = data.country.name
                this.dataForm.address = data.country.address
                this.dataForm.address1 = [data.country.province, data.country.city, data.country.county]
                this.dataForm.contract = data.country.contract
                this.dataForm.lon = data.country.lon
                this.dataForm.lat = data.country.lat
                this.dataForm.createAt = data.country.createAt
              }
            })
          }

          this.$http({
            url: this.$http.adornUrl(`/sys/user/allSysUsers`),
            method: 'get'
          }).then(({data}) => {
            if (data) {
              this.sysUsers = data.users.map(item => {
                return {value: `${item.userId}`, label: `${item.username}:${item.mobile}`}
              })
              console.log(this.sysUsers)
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/country/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'province': this.dataForm.address1[0],
                'city': this.dataForm.address1[1],
                'county': this.dataForm.address1[2],
                'lon': this.dataForm.lon,
                'lat': this.dataForm.lat,
                'contract': this.dataForm.contract,
                'createAt': this.dataForm.createAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            this.options = this.sysUsers.filter(item => {
              return item.label.search(query) > -1
            })
          }, 200)
        } else {
          this.options = []
        }
      }
    }
  }
</script>
